package com.cn.aam.checaiduo.ui.me.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.balysv.materialripple.MaterialRippleLayout;
import com.bumptech.glide.Glide;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.base.adapter.holder.BaseRecyclerViewHolder;
import com.cn.aam.checaiduo.bean.me.InvestRecordB;
import com.cn.aam.checaiduo.bean.me.RedirectContentB;
import com.cn.aam.checaiduo.ui.homepage.pay.PayActivity;
import com.cn.aam.checaiduo.ui.me.RechargeActivity;
import com.cn.aam.checaiduo.ui.me.fragment.InvestRecordFragment;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * monker on 2017/5/26.
 */

public class InvestRecordAdapter extends BaseRecycerAdapter<InvestRecordB> {
    @BindView(R.id.ripple)
    MaterialRippleLayout ripple;
    private String type;

    public InvestRecordAdapter(Context mContext, String type) {
        super(mContext);
        this.type = type;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(View picNewsView) {
        return new InvestRecordHolder(picNewsView);
    }

    @Override
    protected int getItemLayoutRes() {
        return R.layout.grid_investrecord_item;
    }

    @Override
    protected void ItemVIewChange(BaseRecyclerViewHolder recyclerholder, final int position) {
        InvestRecordHolder holder = (InvestRecordHolder) recyclerholder;
        holder.money.setText(getItem(position).getOrder_amount() + "元");
        holder.stage.setText("第" + getItem(position).getSequence() + "期");
        holder.days.setText(getItem(position).getSale_day() + "天");

        //click
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null)
                    onItemClick.onClick(position);
            }
        });

        if (TextUtils.equals(InvestRecordFragment.INVEST_ONSALE, type)) {
            //起息中
            holder.tvTag1.setText("投资金额");
            holder.ll.setVisibility(View.GONE);//起息中没有复利
            holder.date.setText(getItem(position).getPaid_time().split(" ")[0]);
        } else if (TextUtils.equals(InvestRecordFragment.INVEST_PAYMENT, type)) {
            //回款中
            holder.money.setText(parseStr(getItem(position).getReward_amount()) + "元");
            holder.days.setText(parseStr(getItem(position).getReward_day()) + "天");
            holder.tvTag1.setText("回款金额");
            holder.ll.setVisibility(View.VISIBLE);
            holder.tvTag2.setText("单        利");
            holder.date.setText(parseStr(getItem(position).getSi_year_rate()) + "%");
            holder.tvCompound.setText(parseStr(getItem(position).getYear_rate()) + "%");
            holder.days.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            holder.money.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            holder.date.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        } else {
            //募集中
            holder.tvTag3.setText("投资状态");
            holder.ll.setVisibility(View.VISIBLE);
            holder.date.setText(getItem(position).getCreatd_time().split(" ")[0]);


            holder.tvCompound.setText(TextUtils.equals(getItem(position).getOrder_status(), "0") ? "未支付" : "已支付");
            holder.tvCompound.setTextColor(ContextCompat.getColor(mContext, R.color.black_alpha70));
            //如果未支付显示未支付，并且可以去支付
            if (TextUtils.equals("0", getItem(position).getOrder_status())) {
                holder.days.setText("重新支付");
                holder.tvCompound.setTextColor(ContextCompat.getColor(mContext, R.color.textColor2));
                holder.days.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rechargeSuccess(getItem(position).getOrder_id());
                    }
                });
            } else {
                holder.days.setText("募集中");
                holder.days.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            }
        }

        Glide.with(mContext).load(getItem(position).getDefault_image()).crossFade().into(holder.image);

    }


    private String parseStr(String str) {
        if (TextUtils.isEmpty(str)) return "0";
        return str;
    }

    class InvestRecordHolder extends BaseRecyclerViewHolder {
        @BindView(R.id.invest_iv_image)
        ImageView image;
        @BindView(R.id.invest_tv_money)
        TextView money;
        @BindView(R.id.invest_tv_date)
        TextView date;
        @BindView(R.id.invest_tv_stage)
        TextView stage;
        @BindView(R.id.invest_tv_days)
        TextView days;
        @BindView(R.id.invest_ll_compound)
        LinearLayout ll;
        @BindView(R.id.invest_tv_compound)
        TextView tvCompound;
        @BindView(R.id.invest_tag2)
        TextView tvTag2;
        @BindView(R.id.invest_tag1)
        TextView tvTag1;
        @BindView(R.id.invest_tag3)
        TextView tvTag3;


        public InvestRecordHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    /**
     * 充值
     */
    private void rechargeSuccess(final String orderId) {
        //余额不足的情况下，1.提交订单 2.充值 3.再次发起支付请求
        RetrofitHome.getInstance()
                .getInternalApi()
                .pay(AppData.getToken(), orderId)
                .compose(RxSchedulers.<RedirectContentB>toMain())
                .subscribe(new OnRequestListener<RedirectContentB>(((BaseActivity) mContext)) {
                    @Override
                    protected String getProgressBarContent() {
                        return "付款中...";
                    }

                    @Override
                    public void onNext(RedirectContentB value) {
                        ;
                        //投资记录重新付款，余额不足跳充值界面
                        if (TextUtils.equals(value.getMsg(), "余额不足，无法支付订单")) {
                            WMToast.normal("余额不足，请充值");
                            mContext.startActivity(RechargeActivity.newInstance(mContext));
                            return;
                        }
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_FAILED) return;
                        mContext.startActivity(PayActivity.newInstance(mContext, value.getRedirect_content(), "支付", orderId));

                    }
                });
    }
}
